package instruction

import (
	"fmt"

	"gitee.com/microwww/jvm-go/jvm/rtda"
	"gitee.com/microwww/jvm-go/jvm/tool"
)

type Wide struct {
	target Instruction
}

func (w Wide) FetchOperands(bf *tool.Buffer) {
	r, _ := bf.ReadByte()
	switch r {
	case 0x15: // i-load
		ic := &ILoad{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x16: // l-load
		ic := &LLoad{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x17: // f-load
		ic := &FLoad{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x18: // d-load
		ic := &DLoad{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x19: // a-load
		ic := &ALoad{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x36: // i-store
		ic := &IStore{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x37: // l-store
		ic := &LStore{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x38: // f-store
		ic := &FStore{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x39: // d-store
		ic := &DStore{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x3A: // a-store
		ic := &AStore{}
		ic.Index = uint(bf.ReadUint16())
		w.target = ic
	case 0x84: // i-inc
		ic := &IINC{}
		ic.Index = uint(bf.ReadUint16())
		ic.Const = int32(bf.ReadUint16())
		w.target = ic
	default:
		panic(fmt.Sprintf("Unsupport opcode : %x", r))
	}
}
func (w Wide) Execute(fr *rtda.MethodFrame) {
	w.target.Execute(fr)
}
